// Copyright 2015 Boyaa Inc. All Rights Reserved.

package log

import (
	"fmt"
	"github.com/astaxie/beego/logs"
)

// Log levels to control the logging output.
const (
	LevelEmergency = iota
	LevelAlert
	LevelCritical
	LevelError
	LevelWarning
	LevelNotice
	LevelInformational
	LevelDebug
)

// SetLogLevel sets the global log level used by the simple
// logger.
func SetLevel(l int) {
	BeeLogger.SetLevel(l)
}

func SetLogFuncCall(b bool) {
	BeeLogger.EnableFuncCallDepth(b)
	BeeLogger.SetLogFuncCallDepth(3)
}

// logger references the used application logger.
var BeeLogger *logs.BeeLogger

// SetLogger sets a new logger.
func SetLogger(adaptername string, config string) error {
	err := BeeLogger.SetLogger(adaptername, config)
	if err != nil {
		return err
	}
	return nil
}

func Emergency(format string, v ...interface{}) {
	BeeLogger.Emergency(format, v...)
}

func Alert(format string, v ...interface{}) {
	BeeLogger.Alert(format, v...)
}

// Critical logs a message at critical level.
func Critical(format string, v ...interface{}) {
	BeeLogger.Critical(format, v...)
}

// Error logs a message at error level.
func Error(format string, v ...interface{}) {
	BeeLogger.Error(format, v...)
	fmt.Println(v...)
}

// Warning logs a message at warning level.
func Warning(format string, v ...interface{}) {
	BeeLogger.Warning(format, v...)
}

// Deprecated: compatibility alias for Warning(), Will be removed in 1.5.0.
func Warn(format string, v ...interface{}) {
	Warning(format, v...)
}

func Notice(format string, v ...interface{}) {
	BeeLogger.Notice(format, v...)
}

// Info logs a message at info level.
func Informational(format string, v ...interface{}) {
	BeeLogger.Informational(format, v...)
}

// Deprecated: compatibility alias for Warning(), Will be removed in 1.5.0.
func Info(format string, v ...interface{}) {
	Informational(format, v...)
}

// Debug logs a message at debug level.
func Debug(format string, v ...interface{}) {
	BeeLogger.Debug(format, v...)
}

// Trace logs a message at trace level.
// Deprecated: compatibility alias for Warning(), Will be removed in 1.5.0.
func Trace(format string, v ...interface{}) {
	BeeLogger.Trace(format, v...)
}

func init() {
	// init BeeLogger
	BeeLogger = logs.NewLogger(10000)
	err := BeeLogger.SetLogger("file", `{"filename":"test.log"}`)
	//	err := BeeLogger.SetLogger("console", "")
	if err != nil {
		fmt.Println("init file log error:", err)
	}
	SetLogFuncCall(true)
}
